<?php
/*
 * Created on May 12, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  Contact_model extends Model 
{
	/**
	 * @var unknown_type
	 */
	var $id  ;  
	/**
	 * @var unknown_type
	 */
	var $fullname  ;  
	/**
	 * @var unknown_type
	 */
	var $company  ; 
	/**
	 * @var unknown_type
	 */
	var $address  ;  
	/**
	 * @var unknown_type
	 */
	var $phone;
	/**
	 * @var unknown_type
	 */
	var $fax;
	/**
	 * @var unknown_type
	 */
	var $email;
	/**
	 * @var unknown_type
	 */
	var $title;
	/**
	 * @var unknown_type
	 */
	var $content;
	/*
	 * @access	public
	 */
	 var $recei_date;
	/*
	 * @access	public
	 */
	 var $answer_date;
	/*
	 * @access	public
	 */
	 var $status;
	/*
	 * @access	public
	 */
	function Contact_model()
	{
		parent::Model();
	}
		
	function insert(){
		$sql = "INSERT INTO gtb_contact (fullname, company, address, phone, fax, email, title, content, recei_date, status)
				VALUES(".
				$this->db->escape($this->fullname).", " .
				$this->db->escape($this->company).", " .
				$this->db->escape($this->address).", " .
				$this->db->escape($this->phone).", " .
				$this->db->escape($this->fax).", " .
				$this->db->escape($this->email).", " .
				$this->db->escape($this->title).", " .
				$this->db->escape($this->content).", " .
				$this->db->escape($this->recei_date).", " .
				intval($this->status)
				.")";
		$this->db->query($sql);
		return $this->db->insert_id(); 
	}
	
		
	function update($contact){
		 $sql = "UPDATE gtb_contact 
				SET ".
				" status = " .  intval($contact->status) . "," .
				" answer_date = " .  $this->db->escape($contact->answer_date) .
				" WHERE id = " .intval($contact->id);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
		
	function delete($id){
		if(!is_array($id)) $id = array($id);
		$ids = implode(',', $id);
		$sql = "DELETE FROM gtb_contact WHERE id IN ($ids)";
		//$sql = "DELETE FROM gtb_pcategory WHERE id_pcategory = " . intval($id);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function getAllByCode($code_parent = '', $number_record = 0, $offset = 0){
		$result = null;
		
		if($number_record > 0){
			$sql = "SELECT id_pcategory, id_parent_pcategory, code_parent, code,vi_name,en_name, vi_description 
					FROM gtb_pcategory 
					WHERE (code_parent = '".trim($code_parent)."' )
					ORDER BY id_pcategory DESC
					LIMIT $offset, $number_record";
		}
		
		else $sql = "SELECT id_pcategory, id_parent_pcategory, code_parent, code,vi_name,en_name, vi_description 
				FROM gtb_pcategory ORDER BY id_pcategory DESC";
		//echo $sql;
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result;
	}
	
	function getAll($offset = 0, $number_record = 10){
		
		$sql = "SELECT *
				FROM gtb_contact ORDER BY id DESC LIMIT $offset, $number_record";
		
		//echo $sql;
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result;
	}
	
	function countAll(){
		$result = null;
		
		$sql = "SELECT COUNT(*) as num_record 
					FROM gtb_contact 
				";
		
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result[0]->num_record;
	}
	
	function getByParentCode($code_parent){
		$result = null;
		$sql = "SELECT *
				FROM gtb_pcategory WHERE code_parent = ".$this->db->escape($code_parent)." ORDER BY id_pcategory DESC";
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result;
	}
	
	function getMaxPosition($category_parent_id){
		
		$sql = "SELECT MAX(position) as position_max
				FROM category 
				WHERE category_parent_id = $category_parent_id";
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->position_max;
		}
       
		return 0;
	}
	
	function getById($id){
		
		$sql = "SELECT *
				FROM gtb_contact 
				WHERE id = " . intval($id);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}
       
		return null;
	}
	
	
//	
}

// EOF

